package com.wno704.quartz.task;

import com.wno704.quartz.entity.Job;
import com.wno704.quartz.service.JobService;
import com.wno704.system.util.FtpUtil;
import com.wno704.system.util.SFtpUtil;
import com.wno704.system.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SendFtp
 * @Description TODO
 * @Author wno704
 * @Date 2021/5/10 16:47
 * @Version 1.0
 */

@Slf4j
@Component
public class SendFtp {


    @Autowired
    private JobService jobService;

    public void sendDataFtp(String params) throws Exception {
        Job job = jobService.findJob(Long.parseLong(params));
        log.info("{}-{}----start", job.getRemark(), "sendDataFtp");
        log.info("{}----{}----{}----{}----{}----{}----{}", params, job.getTemplateSrcName(), job.getTemplateDstName(), job.getTemplateDstTwoName(), job.getSrcCommd(), job.getDstCommd(), job.getDstTwoCommd());

        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        String srcCmd = job.getSrcCommd();
        String pathFIle = "", dstpath = "";

        File file = null;

        List<String> listDst = new ArrayList<String>();

        String encode = job.getDstTwoCommd();

        if (job.getTransType().equals("1")) {
            listDst.add(job.getDstCommd());
        }
        List<Map<String, Object>> srcData = new ArrayList<Map<String, Object>>();
        srcData = jdbcTemplateSrc.queryForList(srcCmd);
        if (srcData.size() > 0) {
            for (Map<String, Object> mp : srcData) {
                if (!mp.get("dat").toString().equals(""))
                    listDst.add(mp.get("dat").toString());
                pathFIle = mp.get("srcpath").toString();
                dstpath = mp.get("dstpath").toString();
            }
            file = new File(pathFIle);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            if (encode == null || encode.equals("")) {
                FileUtils.writeLines(file, listDst, true);
            } else {
                FileUtils.writeLines(file, encode, listDst, "\r\n", true);
            }

        }
        String[] ipPort = job.getTemplateDstName().split(":");
        String[] userCodePasswd = job.getTemplateDstTwoName().split(":");
        FTPClient ftpClient = FtpUtil.connectFtpServer(ipPort[0], Integer.parseInt(ipPort[1]), userCodePasswd[0], userCodePasswd[1], "UTF-8");
        FtpUtil.uploadFile(ftpClient, file, dstpath);
        FtpUtil.closeFTPConnect(ftpClient);
        log.info("{}-{}----end", job.getRemark(), "sendDataFtp");
    }


    public void sendDataSFtp(String params) throws Exception {
        Job job = jobService.findJob(Long.parseLong(params));
        log.info("{}-{}----start", job.getRemark(), "sendDataSFtp");
        log.info("{}----{}----{}----{}----{}----{}----{}", params, job.getTemplateSrcName(), job.getTemplateDstName(), job.getTemplateDstTwoName(), job.getSrcCommd(), job.getDstCommd(), job.getDstTwoCommd());

        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        String srcCmd = job.getSrcCommd();
        String pathFIle = "", dstpath = "";

        File file = null;

        List<String> listDst = new ArrayList<String>();

        String encode = job.getDstTwoCommd();

        if (job.getTransType().equals("1")) {
            listDst.add(job.getDstCommd());
        }
        List<Map<String, Object>> srcData = new ArrayList<Map<String, Object>>();
        srcData = jdbcTemplateSrc.queryForList(srcCmd);
        if (srcData.size() > 0) {

            for (Map<String, Object> mp : srcData) {
                if (!mp.get("dat").toString().equals(""))
                    listDst.add(mp.get("dat").toString());
                pathFIle = mp.get("srcpath").toString();
                dstpath = mp.get("dstpath").toString();
            }
            file = new File(pathFIle);
            FileUtils.forceMkdirParent(file);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            if (encode == null || encode.equals("")) {
                FileUtils.writeLines(file, listDst, true);
            } else {
                FileUtils.writeLines(file, encode, listDst, "\r\n", true);
            }
        }
        String[] ipPort = job.getTemplateDstName().split(":");
        String[] userCodePasswd = job.getTemplateDstTwoName().split(":");

        SFtpUtil sftp = new SFtpUtil(userCodePasswd[0], userCodePasswd[1], ipPort[0], Integer.parseInt(ipPort[1]));
        sftp.login();
        String fileName = pathFIle.substring((pathFIle.lastIndexOf("\\") == -1 ? pathFIle.lastIndexOf("/") : pathFIle.lastIndexOf("\\")) + 1);
        boolean b = sftp.upload(dstpath, fileName, FileUtils.openInputStream(file));
        sftp.logout();
        log.info("{}-{}--{}--end", job.getRemark(), "sendDataSFtp", b);
    }


    public void sendDataToFile(String params) throws Exception {
        Job job = jobService.findJob(Long.parseLong(params));
        log.info("{}-{}----start", job.getRemark(), "sendDataToFile");
        log.info("{}----{}----{}----{}----{}----{}----{}", params, job.getTemplateSrcName(), job.getTemplateDstName(), job.getTemplateDstTwoName(), job.getSrcCommd(), job.getDstCommd(), job.getDstTwoCommd());

        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        String srcCmd = job.getSrcCommd();
        String pathFIle = "";

        File file = null;

        List<String> listDst = new ArrayList<String>();

        String encode = job.getDstTwoCommd();

        if (job.getTransType().equals("1")) {
            listDst.add(job.getDstCommd());
        }
        List<Map<String, Object>> srcData = new ArrayList<Map<String, Object>>();
        srcData = jdbcTemplateSrc.queryForList(srcCmd);
        if (srcData.size() > 0) {
            for (Map<String, Object> mp : srcData) {
                if (!mp.get("dat").toString().equals(""))
                    listDst.add(mp.get("dat").toString());
                pathFIle = mp.get("srcpath").toString();
            }
            file = new File(pathFIle);
            FileUtils.forceMkdirParent(file);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            if (encode == null || encode.equals("")) {
                FileUtils.writeLines(file, listDst, true);
            } else {
                FileUtils.writeLines(file, encode, listDst, "\r\n", true);
            }
        }
        log.info("{}-{}----end", job.getRemark(), "sendDataToFile");
    }
}
